import yaml
import os
import datetime
import importlib
from ppts.entity.TestConfig import TestConfig


base_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))


# 创建结果目录
def create_current_result_folder():
    path = os.path.join(base_path, 'result')
    folder_name = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
    path = os.path.join(path, folder_name)
    os.makedirs(os.path.join(path, 'screenshot'))
    return path


# 解析根目录下的yaml配置文件,返回TestConfig对象
def parse_config(config_name):
    f = open(os.path.join(base_path, config_name), encoding='utf-8')
    ls = yaml.load(f, Loader=yaml.FullLoader)
    config = TestConfig(config=ls)
    return config


# 执行指定路径的脚本，要传入相关的参数,对应的脚本中也能获取到全局的一些变量参数
def execute_script(path=None, driver=None):
    if path is None or driver is None:
        return
    params = importlib.import_module(path)  # 绝对导入
    return params.to_exec(driver=driver)
